//autoLogin.js
import store from "../../utils/factory";
import { wxLogin, appid, getExpress, getConfig } from "../../utils/config";

Page({
  data: {
    logs: []
  },
  onLoad: function() {
    wx.login({
      success: function(res) {
        console.log(res);
        store.set("loginCode", res.code);
      }
    });
  },
  onShow() {},
  getExpress() {
    getExpress()
      .then(res => {
        let express = res.data;
        store.set("express", express);
      })
      .catch(err => {});
  },
  getConfig() {
    getConfig()
      .then(res => {
        console.log(res.data);
        let req = res.data;
        if (req) {
          store.set("express", req.express);
          store.set("wholeSale", req.wholeSale);
        }
      })
      .catch(err => {});
  },
  getPhoneNumber(e) {
    store.remove("cookie");
    if (e && e.detail && e.detail.encryptedData) {
      wx.showLoading({
        title: "登陆中..."
      });
      wxLogin({
        appId: appid,
        code: store.get("loginCode"),
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
        type: 1
      }).then(res => {
        console.log(res);
        let req = res.data;
        store.set("userInfo", req);
        // this.getExpress();
        this.getConfig();
        setTimeout(function() {
          wx.hideLoading();
          // 跳转到页面 A
          wx.navigateBack({
            delta: 2
          });
        }, 1000);
      });
    }
  }
});
